
<template>
  <el-card>
      <template #header>
        <h2>员工注册</h2>
      </template>
        <el-divier></el-divier>
      <el-form :model="emForm" status-icon :rules="rules" ref="emForm" label-width="150px" class="demo-emForm">
  <el-form-item label="用户名" prop="emName">
    <el-input  v-model="emForm.emName" autocomplete="off" style="width: 45%;"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="emPsw">
    <el-input type="password" v-model="emForm.emPsw" autocomplete="off" style="width: 45%;"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="checkPsw">
    <el-input type="password" v-model="emForm.checkPsw" autocomplete="off" style="width: 45%;"></el-input>
  </el-form-item>
  <el-form-item label="电话号码" prop="emTel">
    <el-input v-model.number="emForm.emTel" style="width: 45%;"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="emSignUpSubmit('emForm')">提交</el-button>
    <el-button @click="resetFormAndToLogin">返回登录界面</el-button>
  </el-form-item>
  <el-form-item>
    <el-link :underline="false" type="primary" @click="toHrSignUp">hr用户注册</el-link>
  </el-form-item>
</el-form>
  </el-card>

</template>

<script>
  export default {
    name:'emSignUp',
    data() {
      var checkEmName = (rule, value, callback) => {//判断用户名不为空
        if (!value) {
          return callback(new Error('用户名'));
        }else{callback();}
        // setTimeout(() => {
        //   if (!Number.isInteger(value)) {
        //     callback(new Error('汉字'));
        //   } else {
        //       callback();
        //   }
        // }, 1000);
      };
      var checkEmTel = (rule, value, callback) => {//判断电话号码不为空
        if (!value) {
          return callback(new Error('电话号码不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入11位数字'));
          } else {
            callback();
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {//判断是否输入了密码
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.emForm.checkPsw !== '') {
            this.$refs.emForm.validateField('checkPsw');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {//判断是否输入了相同的密码
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.emForm.emPsw) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        emForm: {
          emName:'',
          emPsw: '',
          checkPsw: '',
          emTel: ''
        },
        rules: {
          emName:[
            {validator: checkEmName,trigger:'blur' }
          ],
          emPsw: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPsw: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          emTel: [
            { validator: checkEmTel, trigger: 'blur' },
            {            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对',
            trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      emSignUpSubmit(formName) {//em注册功能函数
        this.$refs[formName].validate((valid) => {
          if (valid) {
            
            this.$axios.get("#")
            .then(function(response){
              console.log(response.data)
              alert('注册成功');
            },function(err){
              alert('注册成功失败')
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },

      resetFormAndToLogin() {
        //this.$refs[formName].resetFields();
        this.$router.push({path:'/login'});
      },
      toHrSignUp(){
        this.$router.push({path:'/hrSignUp'});
      }
    }
  }
</script>

<style>

</style>
